package cn.ylz.ylmall.account.dao.persist.repository.impl;

import cn.ylz.ylmall.account.dao.persist.mapper.PermissionMapper;
import cn.ylz.ylmall.account.dao.persist.repository.IPermissionRepository;
import cn.ylz.ylmall.account.pojo.entity.Permission;
import cn.ylz.ylmall.account.pojo.vo.PageData;
import cn.ylz.ylmall.account.pojo.vo.PermissionListVO;
import cn.ylz.ylmall.account.pojo.vo.PermissionStandardVO;
import cn.ylz.ylmall.common.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class PermissionRepositoryImpl implements IPermissionRepository {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public int countByName(String name) {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        return permissionMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(Permission permission) {
        int rows = permissionMapper.insert(permission);
        return rows;
    }

    @Override
    public PermissionStandardVO getStandardById(Long id) {
        return permissionMapper.getStandardById(id);
    }

    @Override
    public int delete(Long id) {
        return permissionMapper.deleteById(id);
    }

    @Override
    public int update(Permission permission) {
        return permissionMapper.updateById(permission);
    }

    @Override
    public PageData<PermissionListVO> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<PermissionListVO> list = permissionMapper.list();
        PageInfo<PermissionListVO> pageInfo = new PageInfo<>(list);
        PageData<PermissionListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

}
